package drds.server.sqlparser.sqlobject.expression;

import java.util.ArrayList;
import java.util.List;

import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class AggregateFunction extends AbstractExpression {

	protected String methodName;

	protected final List<Expression> argumentList = new ArrayList<Expression>();

	public AggregateFunction(String methodName) {
		this.methodName = methodName;
	}

	public String getMethodName() {
		return this.methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public List<Expression> getArgumentList() {
		return this.argumentList;
	}

	public void addArgument(Expression argument) {
		if (argument != null) {
			argument.setParent(this);
		}
		this.argumentList.add(argument);
	}

	@Override
	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.argumentList);
		}

		visitor.endVisit(this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((argumentList == null) ? 0 : argumentList.hashCode());
		result = prime * result + ((methodName == null) ? 0 : methodName.hashCode());

		return result;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null) {
			return false;
		}
		if (getClass() != o.getClass()) {
			return false;
		}
		AggregateFunction other = (AggregateFunction) o;
		if (argumentList == null) {
			if (other.argumentList != null) {
				return false;
			}
		} else if (!argumentList.equals(other.argumentList)) {
			return false;
		}
		if (methodName == null) {
			if (other.methodName != null) {
				return false;
			}
		} else if (!methodName.equals(other.methodName)) {
			return false;
		}

		return true;
	}

}
